package com.example.a0529liuhongzhi;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.a0529liuhongzhi.Adapter.FragmentAdapter;
import com.example.a0529liuhongzhi.Fragment.CarFragment;
import com.example.a0529liuhongzhi.Fragment.HomeFragment;
import com.example.a0529liuhongzhi.Fragment.MeFragment;
import com.example.a0529liuhongzhi.Fragment.SearchFragment;
import com.example.a0529liuhongzhi.Fragment.SortFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private ViewPager viewpage;
    private RadioButton home;
    private RadioButton sort;
    private RadioButton search;
    private RadioButton car;
    private RadioButton me;
    private RadioGroup rb;
    FragmentAdapter fragmentAdapter;

    HomeFragment homeFragment;
    SortFragment sortFragment;
    SearchFragment searchFragment;
    CarFragment carFragment;
    MeFragment meFragment;

    List<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        viewpage = (ViewPager) findViewById(R.id.viewpage);
        home = (RadioButton) findViewById(R.id.home);
        sort = (RadioButton) findViewById(R.id.sort);
        search = (RadioButton) findViewById(R.id.search);
        car = (RadioButton) findViewById(R.id.car);
        me = (RadioButton) findViewById(R.id.me);
        rb = (RadioGroup) findViewById(R.id.rb);
        homeFragment=new HomeFragment();
        sortFragment=new SortFragment();
        searchFragment=new SearchFragment();
        carFragment=new CarFragment();
        meFragment=new MeFragment();
        fragments=new ArrayList<>();
        fragments.add(homeFragment);
        fragments.add(sortFragment);
        fragments.add(searchFragment);
        fragments.add(carFragment);
        fragments.add(meFragment);
        fragmentAdapter=new FragmentAdapter(getSupportFragmentManager(),fragments);
        viewpage.setAdapter(fragmentAdapter);
        viewpage.setCurrentItem(0);

        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        home.setChecked(true);
                        break;
                    case 1:
                        sort.setChecked(true);
                        break;
                    case 2:
                        search.setChecked(true);
                        break;
                    case 3:
                        car.setChecked(true);
                        break;
                    case 4:
                        me.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });


        rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.home:
                        viewpage.setCurrentItem(0);
                        break;
                    case R.id.sort:
                        viewpage.setCurrentItem(1);
                        break;
                    case R.id.search:
                        viewpage.setCurrentItem(2);
                        break;
                    case R.id.car:
                        viewpage.setCurrentItem(3);
                        break;
                    case R.id.me:
                        viewpage.setCurrentItem(4);
                        break;
                }
            }
        });
    }
}
